package chances.system.json;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;

public class RestfulJson{
    
	private Object data;
	private RestfulLink _links;
	private RestfulPage page;
	
	public RestfulJson(){
	}
	
    @SuppressWarnings("unchecked")
    public RestfulJson(HttpServletRequest request, Object object) {
        
        Page<Object> pageData = (Page<Object>)object;
        
        this.data = pageData.getContent();
        
        page = new RestfulPage(
                pageData.getSize(), 
                pageData.getTotalElements(), 
                pageData.getTotalPages(), 
                pageData.getNumber());
        
        
        StringBuffer currentPath = new StringBuffer();
        currentPath.append("http://").append(request.getServerName());
        currentPath.append(":").append(request.getServerPort());
        currentPath.append(request.getContextPath());
        currentPath.append(request.getServletPath());
        
        String queryString = request.getQueryString();
        if (StringUtils.isNotBlank(queryString)) {  
            queryString = queryString.replaceAll("(\\&size=[^&]*)", "");
            queryString = queryString.replaceAll("(\\&page=[^&]*)", "");
            queryString = queryString.replaceAll("(size=[^&]*)", "");
            queryString = queryString.replaceAll("(page=[^&]*)", "");
            queryString = queryString.trim();
        }

        if (StringUtils.isNotBlank(queryString)) {
            currentPath.append("?").append(queryString);
            currentPath.append("&");
        }else{
            currentPath.append("?");
        }
        currentPath.append("size=").append(page.getSize());
        
        StringBuffer firstaUrl = new StringBuffer(currentPath.toString());
        StringBuffer lastUrl = new StringBuffer(currentPath.toString());
        firstaUrl.append("&page=0");
        if(page.getTotalPages()==1)
            lastUrl.append("&page=0");
        else
            lastUrl.append("&page=").append(page.getTotalPages()-1);

        _links = new RestfulLink();
        _links.getFirst().setHref(firstaUrl.toString());
        _links.getLast().setHref(lastUrl.toString());
    }
    
    /**
     * @return the data
     */
    public Object getData() {
        return data;
    }
    /**
     * @param data the data to set
     */
    public void setData(Object data) {
        this.data = data;
    }
    /**
     * @return the _link
     */
    public RestfulLink get_links() {
        return _links;
    }
    /**
     * @param _link the _link to set
     */
    public void set_links(RestfulLink _links) {
        this._links = _links;
    }
    /**
     * @return the page
     */
    public RestfulPage getPage() {
        return page;
    }
    /**
     * @param page the page to set
     */
    public void setPage(RestfulPage page) {
        this.page = page;
    }
}

